The idiomatic expression "to keep someone sane" means to maintain a person's mental stability, prevent them from going insane or losing their mind. It implies that without this action, the person might have gone crazy due to stressful situations or emotional distress.